package com.dys.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @className: com.dys.utils.DbUtils
 * @description: 提供SqlSessionFactory单例方法
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/11/03 21:37
 */
public class DbUtils {
    private static SqlSessionFactory factory;

    public static SqlSessionFactory getInstanceFactory() {
        if (factory == null) {
            InputStream in = null;
            try {
                in = Resources.getResourceAsStream("mybatis-config.xml");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            synchronized (DbUtils.class) {
                factory = new SqlSessionFactoryBuilder().build(in);
            }
        }
        return factory;
    }
}
